package com.cloudsea.yunhaianalysis.controller;


import com.cloudsea.yunhaianalysis.dto.ReportQueryDTO;
import com.cloudsea.yunhaianalysis.service.ReportService;
import com.cloudsea.yunhaianalysis.vo.BusinessHoursVO;
import com.cloudsea.yunhaianalysis.vo.SalesTrendVO;
import com.cloudsea.yunhaianalysis.vo.TopSellingItemVO;
import com.cloudsea.yunhaiutils.common.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

@Tag(name = "商家-经营报表", description = "为商家提供各种经营数据的分析报表")
@RestController
@RequestMapping("/reports")
public class ReportController {

    @Autowired
    private ReportService reportService;

    @Operation(summary = "获取热销商品排行榜")
    @GetMapping("/top-selling")
    // 注意这里，我们暂时不使用 @Valid 和 DTO，而是单独接收参数
    public ApiResponse<List<TopSellingItemVO>> getTopSellingReport(
            @Parameter(description = "门店ID", required = true, schema = @Schema(type = "string")) @RequestParam String storeId,
            @Parameter(description = "开始日期", required = true) @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @Parameter(description = "结束日期", required = true) @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
            @Parameter(description = "排行数量") @RequestParam(defaultValue = "10") Integer topN
    ) {
        // 1. 手动将 String 转换为 Long
        Long storeIdLong;
        try {
            storeIdLong = Long.parseLong(storeId);
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("无效的门店ID格式");
        }

        // 2. 手动组装 DTO 对象 (或者直接传递参数给Service)
        ReportQueryDTO dto = new ReportQueryDTO();
        dto.setStoreId(storeIdLong);
        dto.setStartDate(startDate);
        dto.setEndDate(endDate);
        dto.setTopN(topN);

        // 3. 调用 Service
        List<TopSellingItemVO> reportData = reportService.getTopSellingReport(dto);
        return ApiResponse.success(reportData);
    }

    @Operation(summary = "获取销售趋势数据", description = "根据指定的时间范围，返回每日的营业额和订单数，用于绘制折线图")
    @GetMapping("/sales-trend")
    public ApiResponse<List<SalesTrendVO>> getSalesTrendReport(@Valid ReportQueryDTO dto) {
        List<SalesTrendVO> reportData = reportService.getSalesTrendReport(dto);
        return ApiResponse.success(reportData);
    }

    @Operation(summary = "获取营业时段分析数据", description = "根据指定的时间范围，返回每个小时的订单总数和营业总额")
    @GetMapping("/business-hours")
    public ApiResponse<List<BusinessHoursVO>> getBusinessHoursReport(@Valid ReportQueryDTO dto) {
        List<BusinessHoursVO> reportData = reportService.getBusinessHoursReport(dto);
        return ApiResponse.success(reportData);
    }
}